package cn.hznc.controller.service.inventory.logic;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.controller.service.production.logic.ProductionBomDo;
import cn.hznc.domain.entity.ProductionBomEntity;
import cn.hznc.domain.request.inventory.SingleWarehouseSafetyInventoryParam;
import com.github.pagehelper.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author：Administrator
 * @Date：2023/7/20 15:54
 */
@Getter
@Setter
public class SafetyInventoryLogic{

    private final SafetyInventoryDo safetyInventoryDo;

    private final List<SingleWarehouseSafetyInventoryParam> originList;

    private final List<SingleWarehouseSafetyInventoryParam> updateList;

    public SafetyInventoryLogic(List<SingleWarehouseSafetyInventoryParam> originList, List<SingleWarehouseSafetyInventoryParam> updateList) {
        this.originList = originList;
        this.updateList = updateList;
        this.safetyInventoryDo=new SafetyInventoryDo();
    }

    /**
     * 获取处理后的业务模型
     *
     * @return subProgramDo
     */
    public SafetyInventoryDo getProductionBomDo() {
        return safetyInventoryDo;
    }

    /**
     * 分离增删改数据
     */
    public void buildSafetyInventoryDo(){
        if (CollectionUtil.isEmpty(originList)){
            safetyInventoryDo.addCreateElement(updateList);
            return;
        }
        if (CollectionUtil.isEmpty(updateList)){
            safetyInventoryDo.addDeleteElement(originList);
            return;
        }
        // 比对toolNo判断，对删除，修改，新增
        List<String> existToolNos = originList.stream().map(SingleWarehouseSafetyInventoryParam::getToolNo).filter(StringUtil::isNotEmpty).collect(Collectors.toList());
        List<String> updateToolNos = updateList.stream().map(SingleWarehouseSafetyInventoryParam::getToolNo).filter(StringUtil::isNotEmpty).collect(Collectors.toList());
        for (SingleWarehouseSafetyInventoryParam singleWarehouseSafetyInventoryParam : updateList) {
            // 添加修改元素
            if (existToolNos.contains(singleWarehouseSafetyInventoryParam.getToolNo())) {
                safetyInventoryDo.addUpdateElement(singleWarehouseSafetyInventoryParam);
            }
            // 添加新增元素
            if (StringUtil.isNotEmpty(singleWarehouseSafetyInventoryParam.getToolNo())&&!existToolNos.contains(singleWarehouseSafetyInventoryParam.getToolNo())){
                safetyInventoryDo.addCreateElement(singleWarehouseSafetyInventoryParam);
            }
        }
        for (SingleWarehouseSafetyInventoryParam singleWarehouseSafetyInventoryParam : originList) {
            // 添加删除元素
            if (StringUtil.isNotEmpty(singleWarehouseSafetyInventoryParam.getToolNo())&&!updateToolNos.contains(singleWarehouseSafetyInventoryParam.getToolNo())){
                safetyInventoryDo.addDeleteElement(singleWarehouseSafetyInventoryParam);
            }
        }
        // 比对partno判断，对删除，修改，新增
        List<String> existPartnos = originList.stream().map(SingleWarehouseSafetyInventoryParam::getPartno).filter(StringUtil::isNotEmpty).collect(Collectors.toList());
        List<String> updatePartnos = updateList.stream().map(SingleWarehouseSafetyInventoryParam::getPartno).filter(StringUtil::isNotEmpty).collect(Collectors.toList());
        for (SingleWarehouseSafetyInventoryParam singleWarehouseSafetyInventoryParam : updateList) {
            // 添加修改元素
            if (existPartnos.contains(singleWarehouseSafetyInventoryParam.getPartno())) {
                safetyInventoryDo.addUpdateElement(singleWarehouseSafetyInventoryParam);
            }
            // 添加新增元素
            if (!existPartnos.contains(singleWarehouseSafetyInventoryParam.getPartno())&&StringUtil.isNotEmpty(singleWarehouseSafetyInventoryParam.getPartno())){
                safetyInventoryDo.addCreateElement(singleWarehouseSafetyInventoryParam);
            }
        }
        for (SingleWarehouseSafetyInventoryParam singleWarehouseSafetyInventoryParam : originList) {
            // 添加删除元素
            if (StringUtil.isNotEmpty(singleWarehouseSafetyInventoryParam.getPartno())&&!updatePartnos.contains(singleWarehouseSafetyInventoryParam.getPartno())){
                safetyInventoryDo.addDeleteElement(singleWarehouseSafetyInventoryParam);
            }
        }
    }
}
